﻿using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Missbot.Admin.BLL.Svc;
using Missbot.AIModule.OnlineStore;
using Missbot.Service;

namespace Missbot.Admin.Controllers
{
    [Route("[controller]/[action]")]
    public class DriftBottleItemController : APIBaseController
    {
        public class GetHonorListReq
        {
            public int PageSize { get; set; }
            
            public int PageIdx { get; set; }
            
            public string Keyword { get; set; }
            
            public string ViewMode { get; set; }
        }
        
        [HttpPost]
        public IActionResult GetHonorList(GetHonorListReq req)
        {
            var filterBuilder = new FilterBuilder<DriftBottleHonor>();

            if (!string.IsNullOrEmpty(req.Keyword))
            {
                filterBuilder.And(p => p.Name.Contains(req.Keyword) || p.Items.Any(item => item.Name.Contains(req.Keyword)));
            }

            filterBuilder.And(req.ViewMode switch
                      {
                          "limit"  => p => p.IsLimit,
                          "normal" => p => !p.IsLimit,
                          _        => p => true
                      });

            var list = MongoService<DriftBottleHonor>.Get(filterBuilder.Filter, p => p.Id, false, (req.PageIdx - 1) * req.PageSize, req.PageSize);
            return new JsonResult
                (list.Select(p => new
                                  {
                                      p.Name,
                                      p.IsLimit,
                                      p.Year,
                                      p.Month,
                                      Items = p.Items.Select(item => item.Name).ToArray()
                                  }).ToList());
        }

        [HttpPost]
        public IActionResult InsertHonor(DriftBottleHonor honor)
        {
            honor.Insert();
            return new JsonResult(new {code = 1});
        }

        [HttpPost]
        public IActionResult UpdateHonor(DriftBottleHonor honor)
        {
            honor.Update();
            return new JsonResult(new {code = 1});
        }

        [HttpPost]
        public IActionResult DeleteHonor(string honorID)
        {
            var honor = MongoService<DriftBottleHonor>.GetOnly(p => p.Id == honorID);
            if (honor == null)
            {
                return new JsonResult(new {code = -1, msg = "wrong honorID"});
            }

            MongoService<DriftBottleHonor>.Delete(honor);
            return new JsonResult(new {code = 1});
        }
    }
}